// 屏蔽用户/关键字
// noinspection JSUnusedLocalSymbols

import {NukeApi} from "@ginstone/nga-api/dist/src/method/NukeApi";
import {MyCache} from "@ginstone/common-utils/dist/src/utils/MyCache";

export default {
    namespaced: true,
    state: {
        // 缓存
        cache: new MyCache({limit: 60 * 30}),
    },
    mutations: {
        method(state, payload) {

        },
    },
    actions: {
        method: ({dispatch, commit, state, rootGetters}, payload) => {

        },
        getBlockData: ({dispatch, commit, state, rootGetters}, force) => {
            const client = rootGetters["Account/getClient"]();
            const method = () => NukeApi.blockDataList(client).then(r => r.data)
            return state.cache.use({uid: client.userId}, method, force)
        },
        setBlockData: ({dispatch, commit, state, rootGetters}, data) => {
            const client = rootGetters["Account/getClient"]();
            return NukeApi.blockDataSet(client, data).then(res => {
                state.cache.remove({uid: client.userId})
                return res;
            })
        },
        //添加一个屏蔽的用户
        addUser: ({dispatch, commit, state, rootGetters}, user) => {
            return dispatch("getBlockData").then(async data => {
                data.users.push(user)
                return await dispatch('setBlockData', data)
            })
        },
        //移除一个屏蔽的用户
        delUser: ({dispatch, commit, state, rootGetters}, user) => {
            return dispatch("getBlockData").then(async data => {
                data.users = data.users.filter(i => !i.startsWith(user + '/') && !i.endsWith('/' + user))
                return await dispatch('setBlockData', data)
            })
        },
    },
    getters: {},
}